# -*- coding: utf-8 -*-
#

from google.appengine.api import memcache
from google.appengine.ext import db

class CRUD(db.Model):
	@classmethod
	def create(cls, key):
		"""Create a new entry named key and cache it"""
		kind = str(cls).split(".")[-1]
		data = cls.get_or_insert(key)
		memcache.add(key, data, namespace=kind)
		return data

	@classmethod
	def read(cls, key):
		"""Get an entry named key. Only read the DB if data aren't cached"""
		kind = str(cls).split(".")[-1]
		data = memcache.get(key, namespace=kind)
		if data is not None:
			return data
		else:
			data = cls.get_by_key_name(key)
			memcache.add(key, data, namespace=kind)
			return data

	def update(self):
		"""Update entry in both cache and DB"""
		kind = str(self.__class__).split(".")[-1]
		memcache.set(self.key().name(), self, namespace=kind)
		self.put()
		return self

	def delete(self):
		"""Delete entry in both cache and DB"""
		kind = str(self.__class__).split(".")[-1]
		memcache.delete(self.key().name(), namespace=kind)
		super(CRUD, self).delete()

class Player(db.Model):
	pass

class User(CRUD):
	name	= db.StringProperty()
	uid		= db.StringProperty()
	contact = db.StringListProperty()
	mailList= db.StringListProperty()
	mail	= db.EmailProperty()
	im		= db.IMProperty()
	imPass	= db.StringProperty()
	lastCo	= db.DateTimeProperty(auto_now=True)
	avatar	= db.BlobProperty()
	ptsAme	= db.IntegerProperty()
	nowCity	= db.TextProperty()
	extPass	= db.StringProperty()

	@staticmethod
	def check(name):
		"""Check if the player exist without named key, and return it"""
		old = Player.all().filter('name = ', name).get()
		return old

	@staticmethod
	def convert(uid, old):
		"""Convert old data to new format"""
		new = User.create(uid)

		if old.name is not None: new.name = old.name
		if old.uid is not None: new.uid = old.uid
		if old.contact is not None: new.name = old.name
		if old.mailList is not None: new.mailList = old.mailList
		if old.mail is not None: new.mail = old.mail
		if old.im is not None: new.im = old.im
		if old.imPass is not None: new.imPass = old.imPass
		if old.lastCo is not None: new.lastCo = old.lastCo
		if old.avatar is not None: new.avatar = old.avatar
		if old.ptsAme is not None: new.ptsAme = old.ptsAme
		if old.nowCity is not None: new.nowCity = old.nowCity
		if old.extPass is not None: new.extPass = old.extPass

		old.delete()
		return new
		

class Thread(CRUD):
	#TODO
	author  = db.StringProperty()
	destin	= db.StringProperty()
	subject = db.StringProperty()
	msgs	= db.ListProperty(item_type=db.Key)
	start	= db.DateTimeProperty(auto_now_add=True)
	last	= db.DateTimeProperty()
	stateOut= db.IntegerProperty()
	stateIn	= db.IntegerProperty()

class Mp(CRUD):
	#TODO
	author  = db.StringProperty()
	destin  = db.StringProperty()
	message = db.TextProperty()
	subject = db.StringProperty()
	state   = db.IntegerProperty()
	date    = db.DateTimeProperty(auto_now_add=True)
	secure  = db.BooleanProperty()

class MailList(CRUD):
	owner	= db.Key
	subs	= db.StringListProperty()
	public  = db.BooleanProperty()
	mdp		= db.StringProperty()
